package com.lqg.bookLibrary.adminController;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lqg.bookLibrary.common.core.domain.JsonResult;
import com.lqg.bookLibrary.pojo.Books;
import com.lqg.bookLibrary.pojo.Type;
import com.lqg.bookLibrary.service.IBooksService;
import com.lqg.bookLibrary.service.ITypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/manage/type")
@PreAuthorize("hasAnyRole('a')")
public class ManageTypeController {

    @Autowired
    private ITypeService typeService;

    @Autowired
    private PasswordEncoder passwordEncoder;
    

    @GetMapping("/read/{pageNum}")
    public JsonResult read(@PathVariable int pageNum) {
        long count = typeService.count();
        int maxCun = 20;
        int chu = (pageNum-1)*maxCun;
        List<Type> list = typeService.lambdaQuery()
                .last("limit " + chu + "," + maxCun)
                .list();

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("data", list);
        jsonObject.put("pages", (int)(count/maxCun)+1);
        jsonObject.put("nowPage", pageNum);

        return JsonResult.success(jsonObject);
    }


    @PostMapping("/update")
    public JsonResult update(@RequestBody String post){
        Type type = JSON.parseObject(post, Type.class);
        typeService.updateById(type);
        Type one1 = typeService.lambdaQuery().eq(Type::getTypeId, type.getTypeId()).one();
        return JsonResult.success(one1);
    }


    @GetMapping("/delete/{id}")
    public JsonResult delete(@PathVariable Long id){
        boolean b = typeService.removeById(id);
        if (b) {
            return JsonResult.success();
        }else {
            return JsonResult.fail("删除失败");
        }
    }


    @PostMapping("/insert")
    public JsonResult insert(@RequestBody String post){
        Type type = JSON.parseObject(post, Type.class);
        typeService.save(type);
        Type one = typeService.lambdaQuery()
                .eq(Type::getTypeName, type.getTypeName())
                .one();
        return JsonResult.success(one);
    }

    @PostMapping("/search")
    public JsonResult search(@RequestBody String post){
        JSONObject jsonObject = JSONObject.parseObject(post);
        String condition = jsonObject.get("condition").toString();
        String content = jsonObject.get("content").toString();
        //System.out.printf(condition + "     " + content);

        condition = StrUtil.toUnderlineCase(condition);

        QueryWrapper<Type> wrapper = new QueryWrapper<>();
        wrapper.eq(condition, content);
        List<Type> list = typeService.list(wrapper);

        JSONObject result = new JSONObject();
        result.put("data", list);
        result.put("pages", 1);
        result.put("nowPage", 1);

        return JsonResult.success(result);
    }


}
